package com.ywqf_api.controller;

import com.ywqf_api.dto.exc.ReturnExcution;
import com.ywqf_api.entity.Message;
import com.ywqf_api.entity.MessageUser;
import com.ywqf_api.enums.ReturnEnum;
import com.ywqf_api.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2018/4/2 0002.
 */
@Api(description="message接口",produces="application/json")
@Controller
@RequestMapping("message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    /**
     * 查询消息
     * @return
     */
    @ApiOperation(value = "查询消息接口", httpMethod = "POST", notes = "查询消息接口")
    @RequestMapping(value = "/findMessage" ,method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findMessage (Integer customerId,Integer communityId) {
        List<Message> list = messageService.findMessage(customerId,communityId);
        if(list != null && list.size()>0){
            for(int i= 0;i<list.size();i++){
                Message message = list.get(i);
                Integer releaseUserId = message.getReleaseUserId();
                MessageUser messageUser = messageService.findUser(releaseUserId,communityId);
                if(messageUser != null){
                   message.setCommunityId(messageUser.getCommunityId());
                   message.setCommunityName(messageUser.getCommunityName());
                   message.setCorpId(messageUser.getCorpId());
                   message.setCorpName(messageUser.getCorpName());
                   message.setName(messageUser.getName());
                   message.setRoleId(messageUser.getRoleId());
                   message.setRoleName(messageUser.getRoleName());
                }
            }
            return new ReturnExcution(ReturnEnum.FIND_SUCCESS,list);
        }
        return new ReturnExcution(ReturnEnum.FIND_NULL_MESSAGE,null);
    }


    /**
     * 查询单条消息
     * @return
     */
    @ApiOperation(value = "查询单条消息接口", httpMethod = "POST", notes = "查询单条消息接口")
    @RequestMapping(value = "/findMessageOne" ,method = RequestMethod.POST)
    @ResponseBody
    public ReturnExcution findMessageOne (Integer publicityId) {
        Message find = messageService.findMessageOne(publicityId);
        return new ReturnExcution(ReturnEnum.FIND_SUCCESS,find);
    }




}
